package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.bean.CurrentThreadLocalUserInfo;
import cn.dansj.common.utils.bean.CacheToken;
import cn.dansj.entity.User;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.service.UserService;
import cn.dansj.common.context.ApplicationContextProvider;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.project.ProjectUtils;
import com.alibaba.fastjson2.JSONObject;

import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class CheckUser extends InterfaceHandler {

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        CacheToken cacheToken = CurrentThreadLocalUserInfo.getCurrentThreadLocalUserInfo().getCacheToken();
        String sequence = cacheToken.getSequence();

        if (Verification.checkNull(sequence)) {
            throw new InterfaceResponseException(ResponseCode.NoAuthorize);
        }
        UserService userService = ApplicationContextProvider.getBean(UserService.class);
        User user = userService.getUserBySequence(sequence);
        if (user == null) {
            throw new InterfaceResponseException(ResponseCode.NoAuthorize);
        }
        JSONObject userInfo = new JSONObject();
        userInfo.put("nickname", user.getNickName());
        userInfo.put("avatar", user.getAvatar());
        userInfo.put("role", user.getRole());
        userInfo.put("token", cacheToken.getUuid());
        userInfo.put("bind_wx", Transformation.castToBoolean(user.getOpenId()));
        userInfo.put("mobile", user.getMobile());
        userInfo.put("mail", user.getMail());
        userInfo.put("sequence", user.getSequence());
        userInfo.put("point", user.getPoint());
        userInfo.put("pay_count", user.getPayCount());
        userInfo.put("crt_time", GetTime.stringTime(user.getCrtTime()));
        userInfo.put("api", user.getToken());
        userInfo.put("last_sign_date", user.getLastSignDate());
        return ProjectUtils.encryptByPrivateKey(userInfo.toJSONString());
    }
}